package com.flower8.controller;

import com.flower8.model.Recommend;
import com.flower8.service.RecommendService;
import com.flower8.sysconfig.constants.MsgConstants;
import com.flower8.utils.PageData;
import com.flower8.utils.ftpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 推荐控制层
 * @Author: tanglin
 * @Date: 2019/10/23 11:12
 * @Version: 1.0
 */
@RestController
@RequestMapping("/recommend")
public class RecommendController extends BaseController{
    private static final Logger log = LoggerFactory.getLogger(RecommendController.class);

    @Autowired
    private RecommendService recommendService;

    /**
     * 查询推荐列表
     * @param request
     * @return
     */
    @RequestMapping("/getRecommendList")
    public Object getRecommendList(HttpServletRequest request){
        logBefore(log, "++++++++++ 查询推荐列表 ++++++++++");
        PageData pd = new PageData(request);

        pd.put("start", Integer.parseInt( pd.get("start").toString() ) );
        pd.put("length", Integer.parseInt( pd.get("length").toString() ) );

        Map res = new HashMap();
        try {
            List<Recommend> recommendList =  recommendService.getRecommendList(pd);
            int total = recommendService.getRecommendListCount(pd);
            res.put("data", recommendList);
            res.put("recordsTotal", total);   //dataTable分页需要
            res.put("recordsFiltered", total);//dataTable分页需要
            res.put("draw", pd.get("draw").toString());//dataTable分页需要
            logAfter(log);
            Map<String, Object> dataMap = (Map<String, Object>) responseResultForSuccess(res);
            // 兼容旧网页
            dataMap.put("recordsTotal", total);   //dataTable分页需要
            dataMap.put("recordsFiltered", total);//dataTable分页需要
            dataMap.put("draw", pd.get("draw").toString());//dataTable分页需要
            return dataMap;
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

    /**
     *  根据推荐的id查询
     * @param request
     * @return
     */
    @RequestMapping("/getRecommendById")
    public Object getRecommendById(HttpServletRequest request){
        logBefore(log, "++++++++++  根据推荐的id查询 ++++++++++");
        PageData pd = new PageData(request);

        Map res = new HashMap();
        try {
            Recommend recommend =  recommendService.getRecommendById(pd);
            res.put("recommend", recommend);
            logAfter(log);
            return responseResultForSuccess(res);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

    /**
     * 更新/新增推荐
     * @param recommend
     * @return
     */
    @RequestMapping("/updateRecommend")
    public Object updateRecommend(@RequestBody Recommend recommend){
        try {
            if(recommend.getId() != null){
                logBefore(log, "++++++++++ 更新推荐接口 ++++++++++");
                recommendService.updateRecommend(recommend);
            }else{
                logBefore(log, "++++++++++ 新增推荐接口 ++++++++++");
                recommendService.addRecommend(recommend);
            }

            logAfter(log);
            return responseResultForSuccess(null, "操作成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError("操作失败");
        }
    }

    /**
     * 推荐图标上传
     * @param pic
     * @return
     */
    @RequestMapping("/uploadFile")
    public Object uploadFile(@RequestParam MultipartFile pic){
        logBefore(log, "++++++++++ 推荐图标上传 ++++++++++");
        try {
            String filePath = "";
            filePath = ftpUtil.uploadFile(pic,2);//主要就是这里实现了ftp的文件上传
            if (!"".equals(filePath)) {
                log.info("上传文件成功!");
                logAfter(log);
                Map<String, Object> data = new HashMap<>();
                data.put("url", filePath);
                return responseResultForSuccess(data);
            }else{
                return responseResultForError("上传图片失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError("上传图片失败");
        }
    }

    /**
     * 删除推荐
     * @param id
     * @return
     */
    @RequestMapping("/deleteRecommend")
    public Object deleteRecommend(@RequestParam long id){
        logBefore(log, "++++++++++ 删除推荐 ++++++++++");
        try {
            recommendService.deleteRecommend(id);
            logAfter(log);
            return responseResultForSuccess(null, "删除成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError("删除失败");
        }
    }

}
